Popular Searches
Popular Course Categories
Popular Courses

Understanding JDK, JRE, and JVM

Understanding JDK, JRE, and JVM

Java Installation and Development Environment Setup

Understanding JDK, JRE, and JVM

Course: Core Java Training

Topic: Understanding JDK, JRE, and JVM

Course Link: Core Java Training Course

Demo Link: Register for a Course Demo

Website: Visit JustAcademy


1. Introduction to JDK, JRE, and JVM

Java is a widely used programming language for developing desktop applications, web applications, enterprise software, Android applications, and backend services. To understand how Java programs are developed and executed, it is important to understand three fundamental terms: JDK, JRE, and JVM.

These components are closely related but perform different functions. The JDK provides development tools, the JVM executes Java bytecode, and the JRE is a term traditionally used for the runtime environment that contains the components needed to run Java applications.

Modern Java distributions may package these components differently. Developers generally install a JDK, which includes the tools required to compile and run Java programs. A separate JRE installation is not necessarily available or required.

Learning Objectives

  • Understand the meaning of JDK, JRE, and JVM.
  • Learn the responsibilities of each component.
  • Understand how Java source code becomes executable bytecode.
  • Identify the differences between compilation and execution.
  • Learn how platform independence works in Java.
  • Practice compiling and running Java programs.
  • Understand common Java installation and runtime errors.

2. What Is the JVM?

JVM stands for Java Virtual Machine. It is the runtime engine that loads, verifies, and executes Java bytecode. The JVM provides an execution environment for compiled Java programs and manages important runtime operations.

Java source code is generally compiled into bytecode, which is stored in files with the .class extension. A compatible JVM can execute this bytecode on its supported operating system and processor architecture.

Responsibilities of the JVM

  • Class loading: Loads required classes into the runtime environment.
  • Bytecode verification: Checks bytecode for structural correctness and important safety properties.
  • Execution: Executes bytecode through interpretation and just-in-time compilation.
  • Memory management: Manages runtime memory used by the application.
  • Garbage collection: Reclaims memory occupied by objects that are no longer reachable.
  • Runtime optimization: Optimizes frequently executed code when appropriate.
  • Exception handling: Supports Java's runtime exception-handling mechanisms.
  • Thread management: Provides runtime support for multithreaded Java applications.

Example of JVM Execution

Consider the following Java program:

public class HelloJava {
    public static void main(String[] args) {
        System.out.println("Hello, Java!");
    }
}

When compiled, the program produces a class file containing bytecode. The JVM executes that bytecode when the application is launched.

Output:

Hello, Java!

How Does the JVM Execute Bytecode?

  1. The JVM starts when a Java application is launched.
  2. The class loader loads the required classes.
  3. The bytecode verifier checks the loaded bytecode.
  4. The runtime prepares the application for execution.
  5. The execution engine interprets bytecode or compiles frequently executed code into native machine instructions.
  6. The application executes and produces its results.

3. What Is the JRE?

JRE stands for Java Runtime Environment. Traditionally, it refers to the runtime components needed to execute Java applications without providing the complete collection of Java development tools.

A runtime environment includes the JVM and the supporting Java libraries and runtime components required by an application. The exact packaging depends on the Java distribution and version.

Main Responsibilities of the JRE

  • Provide the runtime needed to execute Java applications.
  • Supply standard Java libraries used by programs.
  • Provide the JVM for bytecode execution.
  • Support runtime operations such as memory management and garbage collection.
  • Provide APIs for collections, input and output, networking, date and time, and other common tasks.

Example of When a Runtime Environment Is Needed

Suppose a developer creates a Java-based employee management application and packages it for other users. Those users need an appropriate Java runtime or an application package that includes the required runtime components.

Users do not ordinarily need the Java compiler to run an already compiled application. However, the application must still have access to the correct runtime and any required external dependencies.

Important Note About Modern Java Versions

Older Java distributions commonly offered a separately installable JRE. With modern Java releases, distributions often provide a JDK rather than a separate JRE download. Developers can also create custom runtime images using tools such as jlink when appropriate.

Therefore, the JRE is useful as a conceptual term for understanding Java runtime components, but you should not assume that every current Java distribution provides a separate JRE installer.

4. What Is the JDK?

JDK stands for Java Development Kit. It is the software package used to develop Java applications. It includes the Java compiler, runtime components, standard libraries, and additional development utilities.

Anyone learning Core Java should install a suitable JDK because it provides the tools needed to write, compile, execute, and troubleshoot Java programs.

Main Components of the JDK

  • Java compiler (javac): Converts Java source code into bytecode.
  • Java launcher (java): Starts Java applications.
  • Runtime components: Support the execution of Java applications.
  • Standard libraries: Provide reusable classes and APIs for common programming tasks.
  • Javadoc: Generates API documentation from specially formatted source comments.
  • JAR tool: Creates and manages Java archive files.
  • JShell: Provides an interactive environment for experimenting with Java code.
  • Debugging and diagnostic tools: Help developers inspect program execution and runtime behavior.

Example of JDK Usage

Suppose a developer writes a file named Calculator.java. The JDK compiler can compile the file using the following command:

javac Calculator.java

If compilation succeeds, the compiler normally generates Calculator.class. The developer can then launch the application using:

java Calculator

This workflow demonstrates why the JDK is necessary for standard Java development.

5. Difference Between JDK, JRE, and JVM

Although these terms are related, they have different purposes in the Java ecosystem.

Feature JDK JRE JVM
Full form Java Development Kit Java Runtime Environment Java Virtual Machine
Primary purpose Develop and run Java applications Provide the components needed to run Java applications Execute Java bytecode
Compiler included Yes, through javac Not traditionally included as a development tool No
Runtime execution Yes Yes, through its runtime components Yes
Development utilities Yes Limited compared with a JDK Not a complete development toolkit
Standard libraries Available Traditionally available Works with runtime libraries
Typical purpose Writing, compiling, testing, and running code Running compiled Java programs Loading and executing bytecode

Simple Way to Remember

  • JDK: Tools to develop Java programs.
  • JRE: The runtime environment concept for running Java programs.
  • JVM: The engine that executes Java bytecode.

6. Relationship Between JDK, JRE, and JVM

Conceptually, the JDK provides the development tools and runtime components required to develop and run Java applications. The traditional JRE concept describes the runtime components, and the JVM is the engine inside that runtime environment.

Java Development Kit (JDK)
|
+-- Development Tools
|   |
|   +-- javac
|   +-- javadoc
|   +-- jar
|   +-- Debugging and Diagnostic Tools
|
+-- Runtime Components
    |
    +-- Java Libraries
    |
    +-- Java Virtual Machine (JVM)
        |
        +-- Class Loading
        +-- Bytecode Verification
        +-- Execution Engine
        +-- Memory Management

This diagram represents the traditional conceptual relationship. Actual packaging and distribution layouts vary across Java versions and vendors.

7. How Java Source Code Is Compiled and Executed

Java follows a compilation and runtime execution process. Understanding this process helps explain how the JDK and JVM work together.

Step 1: Write the Source Code

The developer writes a Java program in a file with the .java extension.

public class Welcome {
    public static void main(String[] args) {
        System.out.println("Welcome to Core Java");
    }
}

Step 2: Compile the Source Code

The developer uses the Java compiler included in the JDK.

javac Welcome.java

The compiler checks the source code and generates bytecode in Welcome.class if compilation succeeds.

Step 3: Load the Bytecode

When the application is launched, the JVM loads the required class files and supporting classes.

Step 4: Execute the Program

The JVM executes the bytecode using its runtime execution mechanisms.

java Welcome

Output:

Welcome to Core Java

Complete Execution Flow

Source Code (.java)
        |
        v
JDK Compiler (javac)
        |
        v
Bytecode (.class)
        |
        v
JVM Loads and Verifies Classes
        |
        v
Execution Engine
        |
        v
Program Output

8. Understanding Java Bytecode

Bytecode is an intermediate instruction format generated by the Java compiler. It is stored in class files and is designed to be executed by a compatible JVM rather than directly by the processor as ordinary native machine code.

Advantages of Bytecode

  • Portability: The same class file can often run on different operating systems with compatible JVMs.
  • Verification: The JVM can verify important properties of loaded bytecode.
  • Runtime optimization: The JVM can optimize frequently executed code.
  • Separation: Source code compilation is separated from execution on a particular platform.
  • Distribution: Compiled Java classes can be packaged into JAR files and distributed with their required dependencies.

Is Bytecode the Same as Machine Code?

No. Bytecode is an intermediate representation intended for the JVM. Machine code consists of processor-specific instructions that a CPU can execute directly. A JVM may interpret bytecode or compile it into native instructions during execution.

9. How Java Achieves Platform Independence

Java is commonly described as platform-independent because Java source code is compiled into bytecode that can run on compatible JVM implementations across different operating systems.

For example, a Java application compiled on Windows may run on Linux or macOS when a compatible JVM and all required dependencies are available.

Example

  1. Write a Java program on Windows.
  2. Compile the source code into bytecode using the JDK.
  3. Transfer the compiled class files or application package to a compatible Linux environment.
  4. Run the application using a compatible Java runtime.

The application may not require recompilation simply because the operating system has changed. However, native libraries, platform-specific file paths, operating system integrations, and other dependencies can affect portability.

10. Memory Management in the JVM

The JVM manages memory used by Java applications. Understanding its main memory areas helps developers understand application performance and common runtime problems.

Important Runtime Memory Areas

  • Heap: Stores objects and arrays allocated during execution.
  • Java stacks: Each thread has a stack used for method frames, local variables, and related execution information.
  • Metaspace: In common HotSpot JVM implementations, stores class metadata in native memory.
  • Program Counter: Each JVM thread has a program counter associated with its current execution state.
  • Native method stacks: May support execution of native methods, depending on the implementation.

Role of Garbage Collection

Garbage collection helps reclaim heap memory occupied by objects that are no longer reachable. It reduces the need for developers to manually release ordinary Java objects.

However, garbage collection does not eliminate every memory problem. Applications can still experience memory leaks when objects remain reachable unnecessarily, and resources such as files or database connections should be closed explicitly.

11. Just-In-Time (JIT) Compilation

Just-in-time compilation is a runtime optimization technique used by many JVM implementations. Instead of interpreting every bytecode instruction indefinitely, the runtime can compile frequently executed code into native machine instructions.

Benefits of JIT Compilation

  • Can improve performance for frequently executed code.
  • Allows optimization based on observed runtime behavior.
  • Works alongside interpretation and other runtime mechanisms.
  • Helps JVM applications achieve strong performance for many workloads.

JIT compilation is one reason Java applications may perform differently during initial execution compared with later execution. The exact behavior depends on the JVM implementation, configuration, and workload.

12. Java Versions and Compatibility

Java evolves through regular releases that introduce language features, APIs, performance improvements, and runtime changes. Some releases are designated long-term support (LTS) releases by particular distributions or support providers.

Why Java Version Compatibility Matters

  • A compiler may generate class files that an older runtime cannot execute.
  • Some language features are available only in specific Java versions.
  • Libraries and frameworks may require a minimum Java release.
  • Production environments should use a supported version appropriate for the application.

Checking the Java Version

java -version
javac -version

The commands display the selected runtime and compiler versions. If they show different major versions, check which executables are being used and how your environment variables are configured.

13. Common Errors Related to JDK, JRE, and JVM

Error Possible Cause Solution
java command not found Java is not installed or the launcher is not on PATH. Install a suitable JDK or correct PATH.
javac command not found The JDK compiler is unavailable or not configured. Verify the JDK installation and its bin directory.
UnsupportedClassVersionError The runtime cannot support the class-file version. Use a compatible runtime or compile for an appropriate target release.
Could not find or load main class The class path, class name, package, or working directory is incorrect. Verify the class path and launch the correct fully qualified class name.
ClassNotFoundException A class required at runtime cannot be located. Check dependencies and the runtime class path or module path.
OutOfMemoryError The application cannot allocate required memory within available limits. Investigate memory usage, object retention, workload size, and JVM settings.
StackOverflowError Excessive stack usage, often caused by deep or unbounded recursion. Review recursion and method-call depth.

14. Practical Example: Using the JDK and JVM

Follow these steps to create, compile, and execute a Java program.

Step 1: Create a Java File

Create a file named StudentInfo.java.

Step 2: Write the Program

public class StudentInfo {
    public static void main(String[] args) {
        String studentName = "Rahul";
        int age = 21;

        System.out.println("Student Name: " + studentName);
        System.out.println("Age: " + age);
    }
}

Step 3: Compile the Program

javac StudentInfo.java

The JDK compiler generates StudentInfo.class if the source code is valid.

Step 4: Execute the Program

java StudentInfo

Expected Output:

Student Name: Rahul
Age: 21

Step 5: Identify Each Component's Role

  • The JDK supplies the compiler used to compile the source code.
  • The generated class file contains Java bytecode.
  • The JVM loads and executes the bytecode.
  • The runtime libraries provide standard classes used by the application.

15. Best Practices for Working with JDK, JRE, and JVM

  • Install a JDK suitable for your development project.
  • Use a supported Java version and check framework compatibility.
  • Verify Java and compiler versions before compiling an application.
  • Understand the difference between source code, bytecode, and native machine instructions.
  • Use suitable JVM diagnostic tools when investigating runtime problems.
  • Manage memory carefully and investigate unexpected memory growth.
  • Close files, database connections, and other external resources properly.
  • Avoid assuming that every Java distribution contains a separately installable JRE.
  • Use appropriate build tools to make compilation and dependency management repeatable.
  • Test applications on the target runtime and deployment environment.

16. Practice Exercises

Beginner Exercises

  1. Write a Java program that displays your name and course.
  2. Compile the program using javac.
  3. Execute the compiled program using java.
  4. Check the installed Java and compiler versions.
  5. Explain the difference between the JDK, JRE, and JVM in your own words.

Intermediate Exercises

  1. Compile a Java program into a separate output directory using the -d option.
  2. Run the class using the -cp option.
  3. Investigate an UnsupportedClassVersionError scenario.
  4. Explain how garbage collection supports memory management.
  5. Describe how bytecode and JVM implementations support portability.

17. Interview Questions and Answers

Q1. What is the full form of JDK?

Answer: JDK stands for Java Development Kit. It provides tools and components for developing Java applications.

Q2. What is the full form of JRE?

Answer: JRE stands for Java Runtime Environment. It traditionally describes the runtime components needed to execute Java applications.

Q3. What is the full form of JVM?

Answer: JVM stands for Java Virtual Machine. It loads and executes Java bytecode.

Q4. Can we compile Java code without the JDK?

Answer: Standard Java source compilation normally requires a JDK or an environment that provides the Java compiler. A runtime-only environment is not sufficient for ordinary compilation.

Q5. Can we run Java programs without a separate JRE installation?

Answer: Yes. Modern JDK distributions include runtime components, and some applications are packaged with custom runtime images. A separate JRE installer is not always necessary.

Q6. Why is the JVM important?

Answer: The JVM provides the execution environment for Java bytecode and supports class loading, verification, memory management, and runtime optimization.

Q7. What is bytecode?

Answer: Bytecode is the intermediate instruction format typically produced by the Java compiler and executed by a compatible JVM.

Q8. What is garbage collection?

Answer: Garbage collection reclaims memory occupied by objects that are no longer reachable, helping manage heap memory automatically.

Q9. What is JIT compilation?

Answer: Just-in-time compilation converts selected bytecode into native machine instructions during runtime to improve execution performance.

Q10. Why is Java platform-independent?

Answer: Java source code is generally compiled into bytecode that can run on compatible JVM implementations across different operating systems, provided the necessary dependencies are available.

18. Quick Revision Checklist

  • JDK means Java Development Kit.
  • JRE means Java Runtime Environment.
  • JVM means Java Virtual Machine.
  • The JDK provides tools for developing Java applications.
  • The JVM executes Java bytecode.
  • The JRE traditionally describes the runtime components required to run Java applications.
  • The javac command compiles Java source code.
  • The java command launches Java applications.
  • Bytecode is stored in class files.
  • Garbage collection helps reclaim unreachable objects.
  • JIT compilation can improve runtime performance.
  • Java compatibility depends on the runtime, APIs, and application dependencies.

19. Conclusion

Understanding JDK, JRE, and JVM is fundamental to learning Core Java. The JDK supplies the tools needed to develop applications, the JVM executes Java bytecode, and the JRE is the traditional term for the runtime environment that supports Java program execution.

These components work together to support Java compilation, execution, memory management, and portability. By installing a JDK and practicing compilation and execution commands, learners can build a strong foundation for more advanced topics such as object-oriented programming, exception handling, collections, JDBC, and application development.

Explore the JustAcademy Core Java Training Course, register through the course demo registration page, or visit the JustAcademy homepage for more information.

whatsapp